import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
 * La clase ProgramExecution agrupa los datos que determinan
 * la forma en que se ejecuta un programa.
 */
public class ProgramExecution extends DataCategory
{
    public ProgramExecution()
    {
    }
    public boolean initData(int id,int value)
    {
        if(id > 0x0F)
            switch(id)
            {
                case MACHINING_CYCLE_STATUS:
                    machCycleStatus = Integer.valueOf(value);
                    return true;
                case BLOCK_EXECUTION:
                    blockExec = Integer.valueOf(value);
                    return true;
                case OPTIONAL_STOP:
                    optStop = Integer.valueOf(value);
                    return true;
                case BLOCK_SKIP:
                    blockSkip = Integer.valueOf(value);
                    return true;
                case DRY_RUN:
                    dryRun = Integer.valueOf(value);
                    return true;
                case MST_LOCK:
                    mstLock = Integer.valueOf(value);
                    return true;
                case XY_AXES_LOCK:
                    xyAxesLock = Integer.valueOf(value);
                    return true;
                case Z_AXIS_NEGLECT:
                    zAxisNeglect = Integer.valueOf(value);
                    return true;
                default:
                    return false;
        }
        return false;
    }
    public boolean initData(int id,long value)
    {
        return false;
    }
    public Component getView(Id viewID)
    {
        JPanel panel = new JPanel();
        switch(viewID)
        {
            case JOB_NAME:
                panel.setLayout(new GridLayout(1,2));
                JPanel p_aux = new JPanel();
                JLabel jobNameLabel = NewComp.getLabel("jobName","titleLabelTFrame","blue");
                jobNameField.setText(jobName);
                p_aux.add(jobNameField);
                panel.add(jobNameLabel);
                panel.add(p_aux);
                return panel;
            default:
                return null;
        }
    }
    public Component getEditor(Id editorID)
    {
        JPanel panel = new JPanel();
        ButtonGroup group = new ButtonGroup();
        switch(editorID)
        {
            case MACHINING_CYCLE_STATUS:
                panel = NewComp.getPanel("machiningCycleStatus","titleBorderBFrame",new GridLayout(1,2));
                JRadioButton mcsFeedhold =NewComp.getRadioButton( 
                    "feedhold", 
                    "titleRButton", 
                    "blue",
                    machCycleStatusOpt[machCycleStatus].equals("feedhold"), 
                    new ActionListener()
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                            InfoManager.addCommand(KbdCmdId.CmdId.CYCLE, KbdCmdId.CmdValue.CYCLE_FEED_HOLD);
                            InfoManager.addDataValue(MACHINING_CYCLE_STATUS,0);
                        }
                    }
                );
                JRadioButton mcsAxesMoving =NewComp.getRadioButton( 
                    "axesMotion", 
                    "titleRButton",
                    "blue",
                    machCycleStatusOpt[machCycleStatus].equals("axesMotion"), 
                    new ActionListener()
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                            InfoManager.addCommand(KbdCmdId.CmdId.CYCLE, KbdCmdId.CmdValue.CYCLE_START);
                            InfoManager.addDataValue(MACHINING_CYCLE_STATUS,1);
                        }
                    }
                );
                group.add(mcsFeedhold);
                group.add(mcsAxesMoving);
                panel.add(mcsFeedhold);
                panel.add(mcsAxesMoving);
                return panel;
            case BLOCK_EXECUTION:
                panel = NewComp.getPanel("blockExecution","titleBorderBFrame",new GridLayout(1,2));
                JRadioButton beSingleBlock =NewComp.getRadioButton( 
                    "singleBlock", 
                    "titleRButton",
                    "blue",
                    blockExecOpt[blockExec].equals("singleBlock"), 
                    new ActionListener()
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                            InfoManager.addCommand(KbdCmdId.CmdId.OPTION, KbdCmdId.CmdValue.OPTION_SINGLE_BLOCK_ON);
                            InfoManager.addDataValue(BLOCK_EXECUTION,0);
                        }
                    });
                JRadioButton beContinuous =NewComp.getRadioButton( 
                    "continuous", 
                    "titleRButton",
                    "blue",
                    blockExecOpt[blockExec].equals("continuous"), 
                    new ActionListener()
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                            InfoManager.addCommand(KbdCmdId.CmdId.OPTION, KbdCmdId.CmdValue.OPTION_SINGLE_BLOCK_OFF);
                            InfoManager.addDataValue(BLOCK_EXECUTION,1);
                        }
                    });
                group.add(beSingleBlock);
                group.add(beContinuous);
                panel.add(beSingleBlock);
                panel.add(beContinuous);
                return panel;
            case OPTIONAL_STOP:
                panel = NewComp.getPanel("optionalStop","titleBorderBFrame",new GridLayout(1,2));
                JRadioButton osStop =NewComp.getRadioButton( 
                    "stop", 
                    "titleRButton",
                    "blue",
                    optStopOpt[optStop].equals("stop"), 
                    new ActionListener()
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                            InfoManager.addCommand(KbdCmdId.CmdId.OPTION, KbdCmdId.CmdValue.OPTION_OPTIONAL_STOP_ON);
                            InfoManager.addDataValue(OPTIONAL_STOP,0);
                        }
                    }
                );
                JRadioButton osSkip =NewComp.getRadioButton( 
                    "skip", 
                    "titleRButton",
                    "blue",
                    optStopOpt[optStop].equals("skip"), 
                    new ActionListener()
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                            InfoManager.addCommand(KbdCmdId.CmdId.OPTION, KbdCmdId.CmdValue.OPTION_OPTIONAL_STOP_OFF);
                            InfoManager.addDataValue(OPTIONAL_STOP,1);
                        }
                    }
                );
                group.add(osStop);
                group.add(osSkip);
                panel.add(osStop);
                panel.add(osSkip);
                return panel;
            case BLOCK_SKIP:
                panel = NewComp.getPanel("blockSkip","titleBorderBFrame",new GridLayout(1,2));
                JRadioButton bsSkip =NewComp.getRadioButton( 
                    "skip", 
                    "titleRButton",
                    "blue",
                    blockSkipOpt[blockSkip].equals("skip"),
                    new ActionListener()
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                            InfoManager.addCommand(KbdCmdId.CmdId.OPTION, KbdCmdId.CmdValue.OPTION_BLOCK_SKIP_ON);
                            InfoManager.addDataValue(BLOCK_SKIP,0);
                        }
                    }
                );
                JRadioButton bsRead =NewComp.getRadioButton( 
                    "read", 
                    "titleRButton",
                    "blue",
                    blockSkipOpt[blockSkip].equals("read"), 
                    new ActionListener()
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                            InfoManager.addCommand(KbdCmdId.CmdId.OPTION, KbdCmdId.CmdValue.OPTION_BLOCK_SKIP_OFF);
                            InfoManager.addDataValue(BLOCK_SKIP,1);
                        }
                    }
                );
                group.add(bsSkip);
                group.add(bsRead);
                panel.add(bsSkip);
                panel.add(bsRead);
                return panel;
            case DRY_RUN:
                panel = NewComp.getPanel("dryRun","titleBorderBFrame",new GridLayout(1,2));
                JRadioButton drDry =NewComp.getRadioButton( 
                    "dry", 
                    "titleRButton",
                    "blue",
                    dryRunOpt[dryRun].equals("dry"), 
                    new ActionListener()
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                            InfoManager.addCommand(KbdCmdId.CmdId.OPTION, KbdCmdId.CmdValue.OPTION_DRY_RUN_ON);
                            InfoManager.addDataValue(DRY_RUN,0);
                        }
                    }
                );
                JRadioButton drNormal =NewComp.getRadioButton( 
                    "normal", 
                    "titleRButton",
                    "blue",
                    dryRunOpt[dryRun].equals("normal"), 
                    new ActionListener()
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                            InfoManager.addCommand(KbdCmdId.CmdId.OPTION, KbdCmdId.CmdValue.OPTION_DRY_RUN_OFF);
                            InfoManager.addDataValue(DRY_RUN,1);
                        }
                    }
                );
                group.add(drDry);
                group.add(drNormal);
                panel.add(drDry);
                panel.add(drNormal);
                return panel;
            case MST_LOCK:
                panel = NewComp.getPanel("mstLock","titleBorderBFrame",new GridLayout(1,2));
                JRadioButton mlLocked =NewComp.getRadioButton( 
                    "locked", 
                    "titleRButton",
                    "blue",
                    mstLockOpt[mstLock].equals("locked"), 
                    new ActionListener()
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                            InfoManager.addCommand(KbdCmdId.CmdId.OPTION, KbdCmdId.CmdValue.OPTION_MST_LOCK_ON);
                            InfoManager.addDataValue(MST_LOCK,0);
                        }
                    }
                );
                JRadioButton mlNormal =NewComp.getRadioButton( 
                    "normal", 
                    "titleRButton",
                    "blue",
                    mstLockOpt[mstLock].equals("normal"), 
                    new ActionListener()
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                            InfoManager.addCommand(KbdCmdId.CmdId.OPTION, KbdCmdId.CmdValue.OPTION_MST_LOCK_OFF);
                            InfoManager.addDataValue(MST_LOCK,1);
                        }
                    }
                );
                group.add(mlLocked);
                group.add(mlNormal);
                panel.add(mlLocked);
                panel.add(mlNormal);
                return panel;
            case XY_AXES_LOCK:
                panel = NewComp.getPanel("xyAxesLock","titleBorderBFrame",new GridLayout(1,2));
                JRadioButton xyLocked =NewComp.getRadioButton( 
                    "locked", 
                    "titleRButton",
                    "blue",
                    xyAxesLockOpt[xyAxesLock].equals("locked"), 
                    new ActionListener()
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                            InfoManager.addCommand(KbdCmdId.CmdId.OPTION, KbdCmdId.CmdValue.OPTION_MACHINE_LOCK_ON);
                            InfoManager.addDataValue(XY_AXES_LOCK,0);
                        }
                    });
                JRadioButton xyNormal =NewComp.getRadioButton( 
                    "normal", 
                    "titleRButton",
                    "blue",
                    xyAxesLockOpt[xyAxesLock].equals("normal"), 
                    new ActionListener()
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                            InfoManager.addCommand(KbdCmdId.CmdId.OPTION, KbdCmdId.CmdValue.OPTION_MACHINE_LOCK_OFF);
                            InfoManager.addDataValue(XY_AXES_LOCK,1);
                        }
                    }
                );
                group.add(xyLocked);
                group.add(xyNormal);
                panel.add(xyLocked);
                panel.add(xyNormal);
                return panel;
            case Z_AXIS_NEGLECT:
                panel = NewComp.getPanel("zAxisNeglect","titleBorderBFrame",new GridLayout(1,2));
                JRadioButton zLocked =NewComp.getRadioButton( 
                    "locked", 
                    "titleRButton",
                    "blue",
                    zAxisNeglectOpt[zAxisNeglect].equals("locked"), 
                    new ActionListener()
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                            InfoManager.addCommand(KbdCmdId.CmdId.OPTION, KbdCmdId.CmdValue.OPTION_MACHINE_LOCK_ON);
                            InfoManager.addDataValue(Z_AXIS_NEGLECT,0);
                        }
                    }
                );
                JRadioButton zNormal = NewComp.getRadioButton( 
                    "normal", 
                    "titleRButton",
                    "blue",
                    zAxisNeglectOpt[zAxisNeglect].equals("normal"), 
                    new ActionListener()
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                            InfoManager.addCommand(KbdCmdId.CmdId.OPTION, KbdCmdId.CmdValue.OPTION_MACHINE_LOCK_OFF);
                            InfoManager.addDataValue(Z_AXIS_NEGLECT,1);
                        }
                    }
                );
                group.add(zLocked);
                group.add(zNormal);
                panel.add(zLocked);
                panel.add(zNormal);
                return panel;
            default:
                return null;
        }
    }
    public String getDataCategoryID()
    {
        return "programExecution";
    }
    public String getValue(Id dataID)
    {
        switch(dataID)
        {
            case JOB_NAME:
                return jobName;
            default:
                return null;
        }
    }
    public boolean setCommandValue(int id,int value)
    {
        return initData( id, value);
    }
    public boolean setCommandValue(int id,long value)
    {
        return initData( id, value);
    }
    public boolean setDataValue(int id,int value)
    {
        return initData( id, value);
    }
    public boolean setDataValue(int id,long value)
    {
        return initData( id, value);
    }
    public void applyChange()
    {
        while( changed.isEmpty() == false)
        {
            switch(changed.get(0))
            {
                case OTHER:
                    break;
                default:
                    break;
            }
            changed.remove(0);
        }
    }
    public void cancelChange()
    {
        while( changed.isEmpty() == false)
        {
            switch(changed.get(0))
            {
                case OTHER:
                    break;
                default:
                    break;
            }
        }
        changed.clear();
    }
    static
    {
        jobName = "default";
        machCycleStatus = new Integer(0);
        blockExec = new Integer(0);
        optStop = new Integer(0);
        blockSkip = new Integer(0);
        dryRun = new Integer(0);
        mstLock = new Integer(0);
        xyAxesLock = new Integer(0);
        zAxisNeglect = new Integer(0);
        jobNameField = NewComp.getTxtField("default", "textFieldTFrame");  
    }
    private static final String[] machCycleStatusOpt = {
        "feedhold",
        "axesMotion"
    };
    private static final String[] blockExecOpt = {
        "singleBlock",
        "continuous"
    };
    private static final String[] optStopOpt = {
        "stop",
        "skip"
    };
    private static final String[] blockSkipOpt = {
        "skip",
        "read"
    };
    private static final String[] dryRunOpt = {
        "dry",
        "normal"
    };
    private static final String[] mstLockOpt = {
        "locked",
        "normal"
    };
    private static final String[] xyAxesLockOpt = {
        "locked",
        "normal"
    };
    private static final String[] zAxisNeglectOpt = {
        "locked",
        "normal"
    };
    private static String jobName;
    private static Integer machCycleStatus;
    private static Integer blockExec;
    private static Integer optStop;
    private static Integer blockSkip;
    private static Integer dryRun;
    private static Integer mstLock;
    private static Integer xyAxesLock;
    private static Integer zAxisNeglect;
    private static JTextField jobNameField;
}